<%@page import="java.lang.reflect.Array"%>
<%@page import="com.award.model.Relationship"%>
<%@page import="com.award.model.Commodity"%>
<%@page import="com.award.jdbc.dao.JdbcDao"%>
<%@page import="com.award.model.User"%>
<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	JdbcDao jd = new JdbcDao();
	User user = null;
	if (null != session.getAttribute("user"))
		user = (User) session.getAttribute("user");
	int CommodityID = Integer.parseInt(request
			.getParameter("CommodityID"));
	Commodity commodity = jd.queryCommodityByID(CommodityID);
	List<Relationship> re_list = jd.queryAllRelationship(CommodityID);
	ArrayList<User> selectPeople_list = new ArrayList<User>();
	ArrayList<Integer> forSelect = new ArrayList<Integer>();
	Integer one = null;
	for (int i = 1; i <= commodity.getTotal(); i++) {
		one = new Integer(i);
		forSelect.add(one);
	}
	for (Relationship re : re_list) {
		one = new Integer(re.getSelectNum());
		forSelect.remove(one);
		selectPeople_list.add(jd.queryEmployeeByID(re.getUserID()));
	}
	Relationship thisRe = null;
	if (null != user)
		thisRe = jd.queryRelationshipByOne(user.getId(), CommodityID);
%>

<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">

<title>Raffle Page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<link href="./css/vendor/bootstrap/css/bootstrap.min.css"
	rel="stylesheet">
<link href="./css/flat-ui.min.css" rel="stylesheet">
<link href="./css/style.min.css" rel="stylesheet">
<link rel="stylesheet"
	href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<script
	src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script
	src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
	<jsp:include page="border/header.jsp" flush="true" />
	<!-- 选择的商品 -->
	<div class="container">
		<div class="title-page" style="text-align: left;">
			<h1>★ BAYONET | MARBLE FADE</h1>
		</div>
		<div id="raffle" class="row">
			<div class="col-lg-3 col-md-4">
				<div class="item">
					<div class="item-header">
						<span class="item-name"><%= commodity.getCommodityName() %></span> 
						<span class="item-state">
						<% if(0==commodity.getAward()){ %>
						正在激烈进行中...
						<%}else if(1==commodity.getAward()){ %>
						已结束，请选择其他商品...
						<%} %>
						</span> <span class="item-image"
							style="background-image: url('<%=commodity.getImgURL()%>');"></span>
						<span class="item-sugprice">建议价格</span><span class="item-price"><%=commodity.getPrice()%></span>
					</div>
					<div class="item-footer">
						<span class="item-slots"><i class="fui-user"> </i><%=commodity.getSelectPeopleNum()%>
							/ <%=commodity.getTotal()%></span>
					</div>
				</div>
				<%if(1==commodity.getAward()&&null==thisRe){ %>
				<button
						class="sbtn sbtn-red sbtn-random">很遗憾...已经结束了...</button>
				<%
					}else if (null == user) {
				%>
				<a href="./jsp/connectionWechat.jsp"><button
						class="sbtn sbtn-red sbtn-random">请登录</button></a>
				<%
					} else if (null == thisRe) {
				%>
				<button class="sbtn sbtn-red sbtn-random" data-toggle="modal"
					data-target="#myModal">点击抽奖</button>
				<form method="post" action="servlet/pickUp">
					<input type="hidden" id="commodityID" name="commodityID"
						value="<%=commodity.getId()%>">
					<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
						aria-labelledby="myModalLabel" aria-hidden="true">
						<div class="modal-dialog">
							<div class="modal-content">
								<div class="modal-header">
									<button type="button" class="close" data-dismiss="modal"
										aria-hidden="true">&times;</button>

									<h4 class="modal-title" id="myModalLabel">选择你的幸运数</h4>
								</div>

								<div class="modal-body">

									<select id="selectNum" name="selectNum">
										<%
											for (Integer i : forSelect) {
										%>
										<option value=<%=i%>><%=i%></option>
										<%
											}
										%>
									</select>

								</div>
								<div class="modal-footer">
									<button type="button" class="btn btn-default"
										data-dismiss="modal">关闭</button>
									<input type="submit" class="btn btn-primary">
								</div>
							</div>
						</div>
					</div>
				</form>
				<%
					} else {
				%>
				<%
					 if (0 == commodity.getAward()) {
				%>
				<button class="sbtn sbtn-red sbtn-random">等待开奖结果</button>
				<form method="post" action="servlet/cancelGame?commodityID=<%= CommodityID %>">
				<br><input type="submit" class="sbtn sbtn-red sbtn-random" value="退出本局"/>
				</form>
				<%
					} else if (1 == commodity.getAward()) {
				%>
				<%
					if (user.getId() == commodity.getAwardUserID()) {
				%>
				<button class="sbtn sbtn-red sbtn-random">您已中奖</button>
				<%
					} else {
				%>
				<button class="sbtn sbtn-red sbtn-random">很遗憾，未中奖</button>
				<%
					}
				%>
				<%
					}
				%>
				<%
					}
				%>
			</div>
			<!-- 选择的用户部分 -->
			<div class="col-lg-9 col-md-8">
				<div class="info-lot">
					Follow <a href="#fakeLink" target="_blank"><b>@GamdomOfficial</b></a>
					to enter the raffle
				</div>
				<ul class="list-lots">
				<% int selectNum=0;
				User uuu=null; %>
				<% if(re_list.size()>0){%>
				<% for(int i=1;i<=commodity.getTotal();i++) {
				selectNum=0;%>
				<% for(Relationship r : re_list){ %>
				<% if(i==r.getSelectNum()){ 
				selectNum=i;
				uuu=jd.queryEmployeeByID(r.getUserID());
				break;}
				%>
				<%} %>
				<% if(0==selectNum){ %>
				<li><a href="javascript:;"><div style="background-image: url(css/eachbackground.jpg)"><%= i %></div></a></li>
				<%}else{ %>
				<li><a href="javascript:;"><span class="extra-number"><%= i %></span>
					<img src="<%= uuu.getUserImgURL() %>"></a></li>
				<%} %>
				<%} %>
				<%}else{ %>
				<% for(int i=1;i<=commodity.getTotal();i++) {%>
				<li><a href="javascript:;"><div style="background-image: url(css/eachbackground.jpg)"><%= i %></div></a></li>
				<%} %>
				<%} %>
				</ul>
				<div class="info-lot">


					<!-- 验证随机数部分 -->
					Winner will be chosen using <a href="//random.org" target="_blank">random.org</a>
					<form method="post" action="https://api.random.org/verify"
						target="_blank">
						<input type="hidden" name="format" value="json" autocomplete="off">
						<input type="hidden" name="random" value="" autocomplete="off">
						<input type="hidden" name="signature" value="" autocomplete="off">
					</form>
				</div>
			</div>
		</div>
	</div>
	<div style="height: 65px;"></div>
	<footer style="width:100%;position:fixed;bottom: 0;">
	<div>Copyright &copy; 2017</div>
	</footer>

	<script src="./js/jquery.min.js"></script>
	<script src="./js/flat-ui.min.js"></script>
</body>
</html>
